package com.basic.common.string;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * @author YanShuang
 * @version v_1.0.1
 * @explain 字符串操作
 * @since 2018/5/8 10:14
 */
public class StringOperationDsUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(StringOperationDsUtil.class);

    /**
     * 去掉全部空格
     *
     * @param val
     * @return
     */
    public static String subSpaceAll(String val) {
        return StringCheckDsUtil.isNotBlank(val) ? val.replace(" ", "") : val;
    }

    /**
     * 去掉首尾空格
     *
     * @param val
     * @return
     */
    public static String subSpaceTrim(String val) {
        return StringCheckDsUtil.isNotBlank(val) ? val.trim() : val;
    }

    /**
     * 下划线转驼峰
     *
     * @param val
     * @return
     */
    public static String underlineToHump(String val) {
        if (StringCheckDsUtil.isBlank(val)) {
            return val;
        }
        val = val.toLowerCase();
        final StringBuffer sb = new StringBuffer();
        Pattern p = compile("_(\\w)");
        Matcher m = p.matcher(val);
        while (m.find()) {
            m.appendReplacement(sb, m.group(1).toUpperCase());
        }
        m.appendTail(sb);
        return sb.toString();
    }

    /**
     * 只显示第index个汉字，其他隐藏为2个星号<例子：李**>
     *
     * @param str
     * @param index 1 为第index位
     * @return
     */
    public static String left(String str, int index) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        String name = StringUtils.left(str, index);
        return StringUtils.rightPad(name, StringUtils.length(str), "*");
    }

    /**
     * 110****58，前面保留3位明文，后面保留2位明文
     *
     * @param str
     * @param index 3
     * @param end   2
     * @return
     */
    public static String around(String str, int index, int end) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        String right = StringUtils.right(str, end);
        int length = StringUtils.length(str);
        String leftPad = StringUtils.leftPad(right, length, "*");
        return StringUtils.left(str, index).concat(StringUtils.right(leftPad, length - index));
    }

    /**
     * 后四位，其他隐藏<例子：****1234>
     *
     * @param str
     * @return
     */
    public static String right(String str, int end) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        return StringUtils.leftPad(StringUtils.right(str, end), StringUtils.length(str), "*");
    }

    /**
     * 字符串拼接
     *
     * @param strs
     * @return
     */
    public static String builder(String... strs) {
        if (strs != null && strs.length > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String str : strs) {
                stringBuilder.append(str);
            }
            return stringBuilder.toString();
        }
        return "";
    }

    /**
     * 转换网页字符串中的图片形式
     *
     * @param html-网页字符串
     * @return
     * @author LS
     * @date 2019/12/7
     */
//    public static String tranformBase64Image(String html) {
//        if (StringCheckDsUtil.isBlank(html)) {
//            return html;
//        }
//        if (!html.contains("<body>") && !html.contains("<p>") && !html.contains("<div>")) {
//            return html;
//        }
//        Element doc = Jsoup.parseBodyFragment(html).body();
//        try {
//            Elements pngs = doc.select("img[src]");
//            QiNiuFileUploadDsUtil fileUploadUtils = QiNiuFileUploadDsUtil.getInstance();
//            for (Element element : pngs) {
//                String imgUrl = element.attr("src");
//                if (imgUrl.startsWith("data")) {
//                    JSONObject jsonObject = fileUploadUtils.uploadImageBase64(imgUrl, new QiNiuReqDsBean());
//                    if (jsonObject != null && jsonObject.get("fileUrl") != null) {
//                        element.attr("src", jsonObject.get("fileUrl").toString());
//                    }
//                }
//            }
//        } catch (Exception e) {
//            LOGGER.info("字节码解析失败");
//        }
//        return doc.toString();
//    }

    /**
     * 首字母转小写
     */
    public static String toLowerCaseFirstOne(String s) {
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * 首字母转大写
     *
     * @param s
     * @return java.lang.String
     */
    public static String toUpperCaseFirstOne(String s) {
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    public static void main(String[] args) {
        System.out.println(left("ylby1234", 1));
        System.out.println(right("123456789", 4));
        System.out.println(around("123456789", 4, 3));
        System.out.println(builder("string", " is suceess"));

        System.out.println(toUpperCaseFirstOne("longtan"));
    }
}
